﻿using AutoMapper;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using VM.Common.Result;
using VM.Entity.EntityDto.Goods;
using VM.Entity.EntityDto.Lunbo;
using VM.Entity.EntityMap.Goods;
using VM.Entity.EntityMap.ICon;
using VM.Entity.EntityMap.Lunbo;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Menu;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IDAL.Goods;

namespace VM.DAL.Goods
{
    public class GoodsDAL : BaseDAL, IGoodsDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }

        public GoodsDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }

        //public  PagingData<getGoodsTypeListDto> getGoodsTypeList(string? name, int? userId, int pageSize, int pageIndex)
        //{
        //    var ListRes = _Client.Queryable<getGoodsTypeListDto>()
        //                 .Includes(a => a.createUser)
        //                 .Includes(a => a.updateUser)
        //                 .WhereIF(name != null && name != "", a => a.name.Contains(name) )
        //                 .OrderBy(a => a.id);

        //    PagingData<getGoodsTypeListDto> result = new PagingData<getGoodsTypeListDto>()
        //    {
        //        dataList = ListRes.ToPageList(pageIndex, pageSize),
        //        pageIndex = pageIndex,
        //        pageSize = pageSize,
        //        recordCount = ListRes.Count(),
        //        searchString=name
        //    };

        //    return result;

        //}

        public bool addGoods(good_mainInfo obj)
        {
            bool boolRes = false;
            good_mainInfo Res = _Client.InsertNav(obj)
                  .Include(a => a.headImgs)
                  .Include(a => a.applyNotes)
                  .Include(a => a.contentInfo)
                  .ExecuteReturnEntity();
            //寻找商品规格的最低价，处理商品表头上最低价的信息。
            if (Res.id > 0) {

                //List<good_spec> goodSpec=obj.goodSpec;
                //double minValue = (double)goodSpec[0].price;
                //for (int i = 1; i < goodSpec.Count; i++)
                //{
                //    if ((double)goodSpec[i].price < minValue)
                //    {
                //        minValue = (double)goodSpec[i].price;
                //    }
                //}
                ////更新商品主表信息
                //int result = _Client.Updateable<good_mainInfo>()
                //             .SetColumns(it => it.price == Convert.ToDecimal( minValue))
                //             .Where(it => it.id == Res.id)
                //             .ExecuteCommand();
                boolRes = true;
            } 
            return boolRes;
        }

        public List<getLunboListDto> getGoodsLunBoList(int lunBoTypeId)
        {
            List<getLunboListDto> Res = new List<getLunboListDto>();
            //if (lunBoTypeId == 1)
            //{
            List<sys_lunbo> listRes =
                _Client.Queryable<sys_lunbo>()//.ToList();

           //.Where(a=>a.position==1)
           .ToList();
            if (listRes.Count > 0)
            {
                for (int i = 0; i < listRes.Count; i++)
                {
                    getLunboListDto getGoodsLunBoListDto = new getLunboListDto();
                    getGoodsLunBoListDto.id = listRes[i].id;
                    getGoodsLunBoListDto.name = listRes[i].name;
                    getGoodsLunBoListDto.imageSrc = listRes[i].imageSrc;
                    Res.Add(getGoodsLunBoListDto);
                }
            }
            //}




            //else if (lunBoTypeId == 2) {

            //    List<good_mainInfo> listRes =
            //           _Client.Queryable<good_mainInfo>()//.ToList();
            //      .Includes(a => a.headImgs)
            //      .Where(a => a.isHuoDongLunBo == 1)
            //      .ToList();
            //    if (listRes.Count > 0)
            //    {

            //        for (int i = 0; i < listRes.Count; i++)
            //        {
            //            getGoodsLunBoListDto getGoodsLunBoListDto = new getGoodsLunBoListDto();
            //            getGoodsLunBoListDto.id = listRes[i].id;
            //            getGoodsLunBoListDto.title = listRes[i].tilte;
            //            getGoodsLunBoListDto.ImgSrc = listRes[i].headImgs[0].mediaSrc.ToString();
            //            Res.Add(getGoodsLunBoListDto);
            //        }
            //    }
            //}
            return Res;



        }

        /// <summary>
        /// 获取商品列表，根据商品类别
        /// </summary>
        /// <param name="goodsTypeId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PagingData<getGoodsListByTypeIdDto> getGoodsListByTypeId(int goodsTypeId, int pageIndex, int pageSize)
        {
            var listRes =
                 _Client.Queryable<good_mainInfo>()//.ToList();
                .Includes(a => a.headImgs)
                .Where(a => a.goodsTypeId == goodsTypeId)
                ;
            PagingData<good_mainInfo> result = new PagingData<good_mainInfo>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
                searchString = goodsTypeId.ToString()
            };
            PagingData<getGoodsListByTypeIdDto> resend = _IMapper.Map<PagingData<good_mainInfo>, PagingData<getGoodsListByTypeIdDto>>(result);

            return resend;
        }

        public getGoodsInfoByIdDto getGoodsInfoById(int goodsId)
        {
            List<good_mainInfo> listRes = _Client.Queryable<good_mainInfo>()//.ToList();
                 .Includes(a => a.createUser)
                 .Includes(a => a.updateUser)
                 .Includes(a => a.headImgs)
                 .Includes(a => a.applyNotes)
                  .Includes(a => a.contentInfo)
                    .Includes(a => a.goodSpec)

                  .Where(a => a.id == goodsId)
                 .ToList();

            good_mainInfo good_MainInfo = listRes.First();
            getGoodsInfoByIdDto resend = _IMapper.Map<good_mainInfo, getGoodsInfoByIdDto>(good_MainInfo);
            return resend;

        }

        public bool addGoodSpec(good_spec objRes)
        {
            bool boolRes = false;
            good_spec Res = _Client.Insertable(objRes)
                  .ExecuteReturnEntity();
            //寻找商品规格的最低价，处理商品表头上最低价的信息。
            if (Res.id > 0)
            {
                //获取所有的当前商品的列表

                List<good_spec> goodspec = _Client.Queryable<good_spec>()
                .Where(a => a.parentId == objRes.parentId).ToList();
                decimal minvalue = objRes.price;
                for (int i = 1; i < goodspec.Count; i++)
                {
                    if (goodspec[i].price < minvalue)
                    {
                        minvalue = goodspec[i].price;
                    }
                }
                //更新商品主表信息
                int result = _Client.Updateable<good_mainInfo>()
                             .SetColumns(it => it.price == minvalue)
                             .Where(it => it.id == objRes.parentId)
                             .ExecuteCommand();
                boolRes = true;
            }
            return boolRes;
        }
    }
}
